package com.sinopec.android;

import java.util.ArrayList;
import java.util.HashMap;

import com.sinopec.android.adapter.ConferenceListAdapter;
import com.sinopec.android.bean.Conference;
import com.sinopec.android.bean.ConferenceBean;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.net.ThreadManger;
import com.sinopec.android.util.SharePreferencesUtil;
import com.sinopec.android.util.StringUtil;
import com.sinopec.android.util.ToastUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


/**
* @ClassName: ConferenceManagementActivity
* @Description: TODO(会议管理activity)
* @author liuwei
* @date 2012-4-16 上午11:08:54
* 
*/
public class ConferenceManagementActivity extends BaseActivity implements OnClickListener,ThreadCallBack{
	private TextView week_date_textview,current_week_result_count_textview,no_result_text_view;
	private ListView listView;
	
	private Button current_week_btn;
	private Button pre_week_btn;
	private Button next_week_btn;
	private TextView current_week_textview;
	
	
	public  ArrayList<Conference>  listData;
	ConferenceListAdapter conferenceListAdapter;
	
	private long count = 0;
	
	private int btn_flag = 0;
	
	int weekRelative =0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conference_management_view);
		initView();
		//调接口
		sendToServer();
	}

	void initView(){
		((TextView)findViewById(R.id.navi)).setText("会议管理");
		week_date_textview = (TextView) findViewById(R.id.week_date_textview);
		current_week_result_count_textview = (TextView) findViewById(R.id.current_week_result_count_textview);
		no_result_text_view = (TextView)findViewById(R.id.no_result_text_view);
		listView = (ListView) findViewById(R.id.conference_management_list);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int postion,
					long id) {
				// TODO Auto-generated method stub
				Conference conference = listData.get(postion);
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), ConferenceDetailActivity.class);
				intent.putExtra("conference", conference);
				startActivity(intent);
			}
		});
		
		current_week_btn = (Button) findViewById(R.id.current_week_btn);
		current_week_btn.setOnClickListener(this);
		
		pre_week_btn = (Button) findViewById(R.id.pre_week_btn);
		pre_week_btn.setOnClickListener(this);
		
		next_week_btn = (Button) findViewById(R.id.next_week_btn);
		next_week_btn.setOnClickListener(this);
		
		current_week_textview = (TextView) findViewById(R.id.current_week_textview);

	}
	@Override
	public void onCallbackFromThread(ResultData resultData) {
		// TODO Auto-generated method stub
		if(resultData!=null && resultData.getResultList()!=null){
		}else{
			if(btn_flag==0){
				
			}else if(btn_flag==1){
				weekRelative++;
			}else if(btn_flag==2){
				weekRelative--;
			}
			Toast.makeText(this,R.string.get_fail, 0).show();
			return;
		}
		if(resultData.getResultList().get(0) instanceof String){
			if(btn_flag==0){
				
			}else if(btn_flag==1){
				weekRelative++;
			}else if(btn_flag==2){
				weekRelative--;
			}
			Toast.makeText(this,resultData.getResultList().get(0).toString(), 0).show();
			return ;
		}
		ArrayList arrayList = resultData.getResultList();
		//会议管理列表结果
		if(arrayList.size()>0&&arrayList.get(0) instanceof ConferenceBean){
			ConferenceBean conferenceBean = (ConferenceBean) arrayList.get(0);
			
			if(conferenceBean!=null){
				count = conferenceBean.getCount();
				
				current_week_result_count_textview.setText("本周有"+count+"条结果");
				
				
				if(conferenceBean.getWeekDate()!=null){
					week_date_textview.setText(conferenceBean.getWeekDate());
				}
				
			
				current_week_textview.setText("第"+conferenceBean.getWeek()+"周");
				
				
				listData = conferenceBean.getConferenceList();
				if(listData.isEmpty()){
					listView.setVisibility(View.GONE);
					no_result_text_view.setVisibility(View.VISIBLE);
				}else{
					listView.setVisibility(View.VISIBLE);
					no_result_text_view.setVisibility(View.GONE);
				}
				if(conferenceListAdapter!=null){
					conferenceListAdapter.listData = listData;
					conferenceListAdapter.notifyDataSetChanged();
				}else{
					conferenceListAdapter = new ConferenceListAdapter(this,listData);
					listView.setAdapter(conferenceListAdapter);
				}
			}
			
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.current_week_btn:
			weekRelative = 0;
			btn_flag = 0;
			sendToServer();
			break;
		case R.id.pre_week_btn:
			weekRelative--;
			btn_flag = 1;
			sendToServer();
			break;
		case R.id.next_week_btn:
			weekRelative++;
			btn_flag = 2;
			sendToServer();
			break;
		default:
			break;
		}
	}
	
	void  sendToServer(){
		String reqUrl =ConfigAttrs.BASE_URL+"MeetingArrange.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT));
		hashMap.put("passWord", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD));
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);
		hashMap.put("weekRelative", weekRelative+"");

		ThreadManger.exeTask(this, ConfigAttrs.CONFERENCE_MANAGEMENT_TASK_FLAG, hashMap, reqUrl);
	}
}
